package com.zxl.app.controller.shoppingcart;

import com.zxl.app.context.UserContext;
import com.zxl.common.Result;
import com.zxl.common.model.bo.shopping.ShoppingDelete;
import com.zxl.common.model.bo.shopping.ShoppingEdit;
import com.zxl.common.model.bo.shopping.ShoppingGoodsNum;
import com.zxl.common.model.vo.goods.GoodsCartVo;
import com.zxl.common.service.SysShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author zxw
 * @date 2023/4/3
 */
@RestController
@RequestMapping("app/shoppingcart")
@Api(tags = {"购物车模块"})
@Slf4j
public class ShoppingCartController {
    @Resource
    private SysShoppingCartService shoppingCartService;


    @ApiOperation("添加")
    @PostMapping("/add")
    public Result add(@Valid @RequestBody ShoppingEdit shoppingEdit){
        shoppingEdit.setMemberId(UserContext.getUserId());
        shoppingCartService.add(shoppingEdit);
        return Result.success();
    }

    @ApiOperation("减少")
    @PostMapping("/lessen")
    public Result lessen(@Valid @RequestBody ShoppingEdit shoppingEdit){
        shoppingEdit.setMemberId(UserContext.getUserId());
        shoppingCartService.lessen(shoppingEdit);
        return Result.success();
    }

    @ApiOperation("批量删除")
    @PostMapping("/delete")
    public Result<List<GoodsCartVo>> delete(@Valid @RequestBody ShoppingDelete shoppingDelete){
        shoppingDelete.setMemberId(UserContext.getUserId());
        List<GoodsCartVo> list = shoppingCartService.delete(shoppingDelete);
        return new Result(list);

    }

    @ApiOperation("修改商品数量")
    @PostMapping("/update_num")
    public Result updateNum(@RequestBody ShoppingGoodsNum shoppingGoodsNum){
        String goodsCode = shoppingGoodsNum.getGoodsCode();
        Integer goodsNum = shoppingGoodsNum.getGoodsNum();
        shoppingCartService.updateNum(UserContext.getUserId(),goodsCode, goodsNum);
        return Result.success();
    }

    @ApiOperation("查看")
    @GetMapping("/list")
    public Result<List<GoodsCartVo>> list(){
        List<GoodsCartVo> memberGoodsCartVo = shoppingCartService.getMemberGoodsCart(UserContext.getUserId());
        return new Result(memberGoodsCartVo);
    }



}
